package com.hui.auth.web;

import com.hui.auth.dto.JsonResult;
import com.hui.auth.entity.AuthMenu;
import com.hui.auth.service.AuthMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static java.awt.SystemColor.menu;

/**
 * 菜单管理控制器
 * Created by LiaoYunHui on 2017/9/29/029.
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping(value = "auth/menu")
public class MenuController {
    @Autowired
    private AuthMenuService menuService;

    /**
     * 新增菜单
     *
     * @return
     */
    @ApiOperation(value = "新增菜单")
    @PostMapping("insert")
    public JsonResult insert
    (AuthMenu menu) {
        JsonResult jsonResult = new JsonResult<>();
        menuService.insert(menu);
        return jsonResult;
    }

    /**
     * 通过ID修改
     *
     * @return
     */
    @ApiOperation(value = "通过ID修改")
    @PostMapping("update")
    public JsonResult update(@RequestBody AuthMenu menu) {
        JsonResult jsonResult = new JsonResult<>();
        menuService.update(menu);
        return jsonResult;
    }

    /**
     * 通过RID修改角色菜单
     *
     * @return
     */
    @ApiOperation(value = "通过RID修改角色菜单")
    @PostMapping("update_role_menu_by_rid")
    public JsonResult updateRoleMenuByRid(@RequestBody Map requestBody) {
        JsonResult jsonResult = new JsonResult<>();
        Integer rid = Integer.valueOf(requestBody.get("rid") + "");
        List<Integer> mids = (List<Integer>) requestBody.get("mids");
        menuService.updateRoleMenuByRid(rid, mids);
        return jsonResult;
    }

    /**
     * 查询树菜单
     *
     * @return
     */
    @ApiOperation(value = "查询树菜单")
    @GetMapping("select_tree")
    public JsonResult<List<AuthMenu>> selectTree() {
        JsonResult<List<AuthMenu>> jsonResult = new JsonResult<>();
        jsonResult.setData(menuService.selectTreeByRoleId());
        return jsonResult;
    }

    /**
     * 查询所有树菜单
     *
     * @return
     */
    @ApiOperation(value = "查询所有树菜单")
    @GetMapping("select_all_tree")
    public JsonResult<List<AuthMenu>> selectAllTree() {
        JsonResult<List<AuthMenu>> jsonResult = new JsonResult<>();
        jsonResult.setData(menuService.selectAllTree());
        return jsonResult;
    }

    /**
     * 删除菜单
     *
     * @return
     */
    @ApiOperation(value = "删除菜单")
    @PostMapping("delete")
    public JsonResult<List<AuthMenu>> delete(Integer id) {
        JsonResult jsonResult = new JsonResult<>();
        if (menuService.delete(id)) {
            jsonResult.setMsg("删除成功");
            return jsonResult;
        }
        jsonResult.setMsg("删除失败");
        return jsonResult;
    }


}
